package com.codi.bus.core.service.impl;

import com.codi.base.util.DateUtils;
import com.codi.bus.core.dao.FundCombinationHistoryDao;
import com.codi.bus.core.domain.FundCombinationHistory;
import com.codi.bus.core.resultModel.FundCombinationHistoryResult;
import com.codi.bus.core.service.FundCombinationHistoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional(readOnly = true)
@Service("fundCombinationHistoryService")
public class FundCombinationHistoryServiceImpl implements FundCombinationHistoryService{
    @Resource
    private FundCombinationHistoryDao fundCombinationHistoryDao;

    @Override
    @Transactional(readOnly = false)
    public int add(FundCombinationHistory fundCombinationHistory) {
        return fundCombinationHistoryDao.add(fundCombinationHistory);
    }

    @Override
    public List<FundCombinationHistoryResult> queryByDate(String type, String combinationCode) {
        Date date = null;

        switch (type.trim()) {
            case "1":
                date = DateUtils.addMondth(new Date(), -1);
                break;
            case "2":
                date = DateUtils.addMondth(new Date(), -3);
                break;
            case "3":
                date = DateUtils.addMondth(new Date(), -12);
                break;
                default:
                    date = null;
        }

        List<FundCombinationHistory> histories = fundCombinationHistoryDao.queryByDate(date, combinationCode);
        List<FundCombinationHistoryResult> results = new ArrayList<>();

        if (histories != null) {
            for (FundCombinationHistory history : histories) {
                FundCombinationHistoryResult result = new FundCombinationHistoryResult();
                BeanUtils.copyProperties(history, result);
                results.add(result);
            }
        }

        return results;
    }

    @Override
    public List<FundCombinationHistory> queryByTradeDay(String combinationCode, Date tradeDate) {
        return fundCombinationHistoryDao.queryByTradeDate(combinationCode.trim(), tradeDate);
    }

    @Override
    public Page<FundCombinationHistory> queryByPage(Pageable pageable) {
        return fundCombinationHistoryDao.queryByPage(pageable);
    }

    @Override
    public Page<FundCombinationHistory> queryByPageAndCombinationCode(Pageable pageable, String combinationCode) {
        return fundCombinationHistoryDao.queryByPageAndCombinationCode(pageable, combinationCode);
    }

    @Override
    @Transactional(readOnly = false)
    public int delete(Long combinationId) {
        return fundCombinationHistoryDao.deleteByKey(combinationId);
    }

    @Override
    public FundCombinationHistory queryById(Long combinationId) {
        return fundCombinationHistoryDao.queryById(combinationId);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateById(FundCombinationHistory fundCombinationHistory) {
        return fundCombinationHistoryDao.updateById(fundCombinationHistory);
    }

    @Override
    @Transactional(readOnly = false)
    public int insertFundHistory(FundCombinationHistory fundCombinationHistory) {
        fundCombinationHistory.setState(1);
        fundCombinationHistory.setLimitMoneyPurchased("");
        fundCombinationHistory.setCreateDate(new Date());
        fundCombinationHistory.setBaseAnnualizedIncome("");
        fundCombinationHistory.setCalDate(new Date());

        return fundCombinationHistoryDao.addFundHistory(fundCombinationHistory);
    }
}
